<?php
/**
 * Created by Silent Night.
 * Author: Silent Night
 * email: silentwolf_wp@163.com
 * Date: 2017/9/28
 * Time: 9:36
 */

namespace app\admin\model;

use think\Model;
use think\Validate;

class Power extends Model{

    protected $field = [
        'id'      => '编号',
        'action' => '路由',
        'created_at'   => '创建时间',
        'updated_at'   => '更新时间',
    ];
    protected $rule = [
        'action' => "require",  //必填。
    ];

    /**
     * 验证数据
     * @return array
     */
    public function valid(){
        $data = input('post.');
        $validate = new Validate($this->rule, [ ], $this->field);
        $res = $validate->check($data);
        //验证字段
        if($res == false){
            $this->_error = $validate->getError();

            return array( 'err' => 1, 'msg' => $this->_error );
        }else{
            return array( 'err' => 0 );
        }
    }


    /**
     * 列表数据
     */
    public function getLists()
    {
        return $this->select();
    }

    /**
     * 注册数据
     */
    public function add(){
        $data = input('post.');
        $data['created_at'] = time();
        $data['updated_at'] = time();
        return $this->allowField(true)->save($data);
    }

    /**
     * 编辑根据id获取全部数据
     */
    public function getListById()
    {
        return $this->where([ 'id' => input('id') ])->find()->toArray();
    }

    /**
     * 编辑用户
     */
    public function edit()
    {
        $data = input('post.');
        $data['updated_at'] = time();
        return $this->allowField(true)->save($data, [ 'id' => input('post.id')]);
    }

    /**
     * 删除用户
     */
    public function delete()
    {
        return $this->where('id', 'in', input('id'))->delete();
    }
}

